<template>
  <div class="flex-box">
    <div
      class="flex-box"
    >
      <el-table
        ref="elTable"
        v-loading="searching"
        :data="data"
        :cell-class-name="classNameHandle"
        :border="border"
        :load="load"
        :lazy="lazy"
        size="mini"
        :row-key="rowKey"
        :tree-props="treeProps"
        :default-expand-all="defaultExpandAll"
        show-overflow-tooltip
        stripe
        height="100%"
        highlight-current-row
        style="width: 100%;margin-bottom: 12px;"
        @selection-change="$emit('selection-change', $event)"
      >
        <slot />
      </el-table>
    </div>

    <el-pagination
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="size"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { deepCopy, errorMessage } from './utils'

export default {
  props: {
    remoteMethod: {
      type: Function,
      required: true
    },
    // eslint-disable-next-line vue/require-default-prop
    load: {
      type: Function
    },
    lazy: {
      type: Boolean,
      default: false
    },
    treeProps: {
      type: Object,
      default: () => {}
    },
    defaultExpandAll: {
      type: Boolean,
      default: false
    },
    pageSize: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default: () => [20, 30, 40, 50]
    },
    rowKey: {
      type: String,
      default: ''
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    pageIndexName: {
      type: String,
      default: 'pageIndex'
    },
    totalCountName: {
      type: String,
      default: 'recordsCount'
    },
    dataName: {
      type: String,
      default: 'data'
    },
    classNameHandle: {
      type: [String, Function],
      default: ''
    },
    border: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      postData: {},
      searching: false,
      data: [],
      currentPage: 1,
      size: this.pageSize,
      total: 0
    }
  },
  methods: {
    loadData(params) {
      if (params) {
        this.currentPage = params.pageIndex || 1
      } else {
        this.currentPage = 1
      }
      return this._loadData(params)
    },
    async _loadData(params) {
      if (!this.remoteMethod) {
        return null
      }
      if (params) {
        this.postData = deepCopy(params)
      }
      this.postData = this.postData || {}
      this.postData[this.pageIndexName] = this.currentPage
      this.postData.pageSize = this.size
      this.searching = true
      try {
        const response = await this.remoteMethod(this.postData)
        if (response) {
          const data = response[this.dataName]
          const recordsCount = response[this.totalCountName]
          this.data = data
          this.total = recordsCount
        }
        this.searching = false
        return response
      } catch (e) {
        if (e.name === 'CancelError') return null
        errorMessage.show(e)
        this.data = []
        this.total = 0
        this.searching = false
        return null
      }
    },
    async reload() {
      return this._loadData(this.postData)
    },
    handleSizeChange(size) {
      this.size = size
      this._loadData()
    },
    handleCurrentChange(page) {
      this.currentPage = page
      this._loadData()
    }
  }
}
</script>

<style scoped>
  .flex-box {
    height: 100%;
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    position: relative;
    z-index: 0;
  }
</style>
